package com.missfresh.cooperation2meituan.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.missfresh.cooperation2meituan.vo.Constant;


public class CommonUtil {
	
	public static String dateToString(Date time){ 
	    SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 
	    String ctime = formatter.format(time); 
	    return ctime; 
	} 
	
	public static void iteratorMap(Map<String, Object> map){
		for (Map.Entry<String, Object> entry : map.entrySet()) {  
		    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());  
		} 
	}
	
	public static Map<String,Object> preparePage(Map<String,Object> params) {
		if(params==null){
			params = new HashMap<String,Object>();;
			params.put("lmtstart", 0);
			params.put("lmtend", Integer.MAX_VALUE);
			return params;
		}
		int pageSize = Constant.PAGESIZE;
		Integer page =null;
		//如果page为0或空，代表查询所有数据
		Object passPage = params.get("page");
		if(passPage!=null){
			page =  (Integer)passPage;
		}
		Integer passpageSize =  (Integer) params.get("pagesize");
		if(passpageSize!=null){
			pageSize = passpageSize;
		}
		if(page ==null || page==0){//查询全部
			page = 1;
			pageSize = Integer.MAX_VALUE;
		}
		params.put("lmtstart", (page-1)*pageSize);
		params.put("lmtend", pageSize);
		return params;
	}
	
	public static int calculateTotalPages(int totals ,Map<String, Object> params){
		int pageSize = Constant.PAGESIZE;
		if(params!=null){
			if(params.get("pageSize")!=null){
				pageSize = (Integer) params.get("pageSize");
			}
		}
		return (totals%pageSize==0)?(totals/pageSize):(totals/pageSize+1);
	}
	
	public static String after2HourDate(Date time){
		Calendar calendar = Calendar.getInstance();
		/* HOUR_OF_DAY 指示一天中的小时 */
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 2);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ctime = df.format(calendar.getTime());
		return ctime; 
	}

	public static void main(String[] args){
		System.out.println(dateToString(new Date()));
		System.out.println(after2HourDate(new Date()));
	}

}
